/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-01-31
 * Time:10:22
 */
public class Solution {
        public static int removeElement(int[] nums, int val) {
            //遇到等于val的元素，就往后找不等于val的a，然后让a把那个元素给覆盖掉
            //遇到某个数字val，就标记出来
            int len = nums.length;
            if(len == 0)
                return 0;
            int quick = 0,slow = 0;  //双指针
            int count = 0;  //计数器，每移除一个元素count就加1，最终新数组长度就是len-count

            while(quick < len) {
                //让slow停在val处
                while(slow < len && quick < len-1 && nums[slow] != val) {
                    ++slow;
                    if(quick < slow) {
                        ++quick;
                    }
                }

                //quick继续走，直到遇到某个不是val的元素，然后让它把那个元素给覆盖掉
                while(quick < len-1 && nums[quick] == val) {
                    ++quick;
                }
                nums[slow++] = nums[quick++];
                ++count;
            }
            return len-count;
        }
}
